<?php

namespace App\Repository;

use App\Consts\GlobalConst;
use App\Consts\GlobalNoteConst;
use App\Models\IndexSearchCollect;
use App\Models\UserCollect;
use Illuminate\Database\Eloquent\Builder;

class CollectRepository extends BaseRepository
{
    /**
     * 获取用户收藏列表
     * @param $searchParams
     * @return Builder
     */
    public function getUserCollectList($searchParams)
    {
        $provider = UserCollect::getInstance($searchParams['user_id'])
            ->where('user_id', $searchParams['user_id'])
            ->where('is_deleted', GlobalConst::IS_NOT_DELETE)
            ->whereIn('note_type', [GlobalNoteConst::TYPE_ARTICLE_NOTE, GlobalNoteConst::TYPE_LINE_NOTE]);

        if (array_key_exists('last_id', $searchParams)) {
            $id = UserCollect::getInstance($searchParams['user_id'])
                ->select(['id'])
                ->where('collect_id', $searchParams['last_id'])
                ->where('user_id', $searchParams['user_id'])
                ->value('id');
            if ($id) {
                $provider->where('id', '<', $id);
            }
        }

        $provider->orderByDesc('updated_at');
        $provider->orderByDesc('id');

        return $this->setPagination($provider);
    }

    /**
     * 新增收藏记录
     * @param $loadData
     * @return UserCollect|\Illuminate\Database\Eloquent\Builder|null
     */
    public function addCollect($loadData)
    {
        return UserCollect::getModel($loadData['user_id'])->create($loadData);
    }

    /**
     * 【暂时】新增[index_search_collect]记录
     * @param $loadData
     * @return UserCollect|\Illuminate\Database\Eloquent\Builder|null
     */
    public function addSearchCollect($loadData)
    {
        return IndexSearchCollect::getModel()->create($loadData);
    }

    /**
     * 【暂时】删除[index_search_collect]记录
     * @param $collectId
     * @param $userId
     * @return bool|int
     */
    public function delSearchCollect($collectId, $userId)
    {
        return IndexSearchCollect::getModel()
            ->where('collect_id', $collectId)
            ->where('user_id', $userId)
            ->update(['is_deleted' => GlobalConst::IS_DELETED]);
    }

    /**
     * 修改收藏记录
     * @param UserCollect $collect
     * @param $loadData
     * @return int|bool|UserCollect
     */
    public function editCollect(UserCollect $collect, $loadData)
    {
        return $collect->updateFormat($loadData);
    }

    /**
     * 修改收藏记录
     * @param IndexSearchCollect $searchCollect
     * @param $loadData
     * @return int|bool
     */
    public function editSearchCollect(IndexSearchCollect $searchCollect, $loadData)
    {
        return $searchCollect->updateFormat($loadData);
    }

    /**
     * 通过收藏ID获取记录
     * @param $userId
     * @param $collectId
     * @return \Illuminate\Database\Eloquent\Builder|UserCollect|object|null
     */
    public function getCollectByUserIdAndCollectId($userId, $collectId)
    {
        return UserCollect::getInstance($userId)
            ->where('user_id', $userId)
            ->where('collect_id', $collectId)
            ->first();
    }

    /**
     * 获取某用户的某主体收藏记录
     * @param $userId
     * @param $targetId
     * @return \Illuminate\Database\Eloquent\Builder|UserCollect|object|null
     */
    public function getCollectByUserIdAndTargetId($userId, $targetId)
    {
        return UserCollect::getInstance($userId)
            ->where('user_id', $userId)
            ->where('target_id', $targetId)
            ->first();
    }

    /**
     * 找到用户对于某主体的正常收藏记录
     * @param $userId
     * @param $targetId
     * @return UserCollect|Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getNormalCollectByTarget($userId, $targetId)
    {
        return UserCollect::getInstance($userId)
            ->where('user_id', $userId)
            ->where('target_id', $targetId)
            ->where('is_deleted', GlobalConst::IS_NOT_DELETE)
            ->first();
    }
}
